﻿using Habi.Solver.Helpers;
using Habi.Solver.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Habi.Solver.Services
{
    public interface IDeviceService
    {
        NetWorkModel FirstNetWork();
        OSModel GetOsInfo();
        DriveModel FirstDrive();
    }

    public class DeviceService : IDeviceService
    {
        private readonly HardwareHandler hardwareHandler;
        public DeviceService()
        {
            hardwareHandler = new HardwareHandler();
        }

        public DriveModel FirstDrive()
        {
            var drives = hardwareHandler.DiskDriveInfo();
            if (drives.Any())
            {
                var drive = new DriveModel();
                drive.SerialNumber = drives.First()["SerialNumber"].ToString();
                drive.Size = string.Format("{0}Gb", Math.Round(Convert.ToDouble(drives.First()["Size"]) / 1024 / 1024 / 1024, 0));

                return drive;
            }

            throw new Exception("Get system drives failed.");
        }

        public NetWorkModel FirstNetWork()
        {
            var networks = hardwareHandler.NetMacInfo();

            if (networks.Any())
            {
                NetWorkModel netWork = new NetWorkModel();
                netWork = new NetWorkModel();
                netWork.Mac = networks.First()["MACAddress"].ToString();
                netWork.IPAddress = ((string[])networks.First()["IPAddress"])[0];

                return netWork;
            }
            throw new Exception("Not find any net work adapter.");
        }

        public OSModel GetOsInfo()
        {
            var osInfos = hardwareHandler.OsInfo();

            if (osInfos.Any())
            {
                var os = new OSModel();
                os.Name = osInfos.First()["Name"].ToString();
                os.Version = osInfos.First()["Version"].ToString();

                return os;
            }

            throw new Exception("Get system OS info failed.");
        }
    }
}
